package io.renren.modules.feedback.service.impl;

import io.renren.common.utils.ConstantUtils;
import io.renren.common.utils.R;
import io.renren.modules.feedback.entity.FeedbackinfosCustomEntity;
import io.renren.modules.projectinfo.dao.ProjectinfosDao;
import io.renren.modules.user.dao.UserinfosDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.renren.modules.feedback.dao.FeedbackinfosDao;
import io.renren.modules.feedback.entity.FeedbackinfosEntity;
import io.renren.modules.feedback.service.FeedbackinfosService;



/**
 * @author 黄清
 */
@Service("feedbackinfosService")
public class FeedbackinfosServiceImpl implements FeedbackinfosService {



	@Autowired
	private FeedbackinfosDao feedbackinfosDao;

	@Autowired
	private ProjectinfosDao projectinfosDao;

	@Autowired
	private UserinfosDao userinfosDao;


	//api提交反馈
	@Override
	public void submitFeedback(FeedbackinfosEntity feedbackinfosEntity) {
		//必要操作
		//添加创建时间
		feedbackinfosEntity.setCreatetime(new Date());
		//生成id
		String id = UUID.randomUUID().toString().replaceAll("-", "");
		feedbackinfosEntity.setFeedbackinfoid(id);
		//设置默认审核状态
		feedbackinfosEntity.setAuditstatus(ConstantUtils.AUDIT);
		//设置默认是否阅读
		feedbackinfosEntity.setIsread(ConstantUtils.READ);
		feedbackinfosDao.save(feedbackinfosEntity);
	}

	@Override
	public List<FeedbackinfosCustomEntity> queryAuditFeedbacks(Map<String, Object> map) {
		return feedbackinfosDao.queryAuditFeedbacks(map);
	}

	@Override
	public int queryAuditTotal(Map<String, Object> map) {
		return feedbackinfosDao.queryAuditTotal(map);
	}

	@Override
	public int audit(FeedbackinfosEntity feedbackinfos) {
		return feedbackinfosDao.audit(feedbackinfos);
	}

	@Override
	public FeedbackinfosCustomEntity queryAuditInfo(String id) {
		return feedbackinfosDao.queryAuditInfo(id);
	}

	@Override
	public FeedbackinfosEntity queryObject(String feedbackinfoid){
		return feedbackinfosDao.queryObject(feedbackinfoid);
	}

	@Override
	public FeedbackinfosCustomEntity queryCustomInfo(String id) {
		return feedbackinfosDao.queryCustomInfo(id);
	}

	@Override
	public List<FeedbackinfosEntity> queryList(Map<String, Object> map){
		return feedbackinfosDao.queryList(map);
	}

	@Override
	public List<FeedbackinfosCustomEntity> queryCustomList(Map<String, Object> map) {
		return feedbackinfosDao.queryCustomList(map);
	}

	@Override
	public int queryAuditStatus(String id) {
		return feedbackinfosDao.queryAuditStatus(id);
	}

	@Override
	public int queryTotalById(Map<String, Object> map) {
		return feedbackinfosDao.queryTotalById(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map){
		return feedbackinfosDao.queryTotal(map);
	}
	
	@Override
	public void save(FeedbackinfosEntity feedbackinfos){
		//创建uuid,设置反馈id
		UUID uuid = UUID.randomUUID();
		String id = uuid.toString().replaceAll("-", "");
		feedbackinfos.setFeedbackinfoid(id);

		//设置反馈创建时间
		Date date = new Date();
		feedbackinfos.setCreatetime(date);
		feedbackinfosDao.save(feedbackinfos);
	}
	
	@Override
	public void update(FeedbackinfosEntity feedbackinfos){


		feedbackinfosDao.update(feedbackinfos);
	}
	
	@Override
	public void delete(String feedbackinfoid){
		feedbackinfosDao.delete(feedbackinfoid);
	}
	
	@Override
	public void deleteBatch(String[] feedbackinfoids){
		feedbackinfosDao.deleteBatch(feedbackinfoids);
	}
	//判断审核状态码和反馈回复问题
	@Override
	public R judgeFeedbackAuditError(FeedbackinfosEntity feedbackinfos, Long userid){
		Integer state = feedbackinfos.getAuditstatus();
		//验证审核状态码标识
		if(state != ConstantUtils.AUDIT && state != ConstantUtils.AUDITED && state != ConstantUtils.AUDITFAILURE){
			return R.error("审核标识错误！");
		}
		String dealcontexts = feedbackinfos.getDealcontexts();
		if((state == ConstantUtils.AUDITED || state == ConstantUtils.AUDITFAILURE)){
			if (dealcontexts == null || "".equals(dealcontexts.trim())) {
				//没有反馈回复
				return R.error("请输入反馈回复内容");
			}
			//设置审核人
			feedbackinfos.setDealuser(userid);
		}else if(state == ConstantUtils.AUDIT){
			//待解决
			//数据库中处理内容内容清空
			feedbackinfos.setDealcontexts("");
		}
		//待审核也存在审核时间
		feedbackinfos.setAuditdate(new Date());
		return null;
	}
	//判断是否阅读状态码错误
	@Override
	public R judgeFeedbackIsReadError(FeedbackinfosEntity feedbackinfos){
		//是否已读状态码是否正确
		Integer isRead = feedbackinfos.getIsread();
		if(!ConstantUtils.READ.equals(isRead) &&  !ConstantUtils.READED.equals(isRead)){
			return R.error("是否读取状态码");
		}
		return null;
	}
	//判断反馈类型错误
	@Override
	public R judgeFeedbackTypeError(FeedbackinfosEntity feedbackinfos){
		//系统反馈
		Integer feedbacktype = feedbackinfos.getFeedbacktype();
		if(ConstantUtils.SYSTEMFEEDBACK.equals(feedbacktype)){
			feedbackinfos.setTargetid("");
		}else if(ConstantUtils.PROJECTFEEDBACK.equals(feedbacktype)){
			//外键关联判空
			//项目名判空
			if(projectinfosDao.isProjectIdExist(feedbackinfos.getTargetid()) == 0){
				return R.error("项目不存在！");
			}
		}else {
			return R.error("反馈类型错误！");
		}
		//为系统反馈时，去除目标id
		if(feedbacktype.equals(ConstantUtils.SYSTEMFEEDBACK)){
			feedbackinfos.setTargetid(null);
		}
		return null;
	}
}
